package org.dioib.practice.common;

import lombok.Data;
import org.dioib.practice.common.constant.Serial;
import org.dioib.practice.common.constant.StatusCode;

import java.time.LocalDateTime;

/**
 * dioib created by 2025
 * @param <T>
 */

@Data
public class Result<T> {

    private final Serial serial;
    private final int code;
    private String message;
    private String path;
    private LocalDateTime timestamp;
    private final T data;

    /**
     * @Deprecated 不建议使用，防止与StatusCode定义不一致
     * @param serial
     * @param code
     * @param message
     * @param data
     */
    @Deprecated
    public Result(Serial serial, int code, String message, T data) {
        this.serial = serial;
        this.code = code;
        this.message = message;
        this.data = data;
    }
    public Result(StatusCode state, T data) {
        this.serial = state.getSerial();
        this.code = state.getCode();
        this.message = state.getDesc();
        this.data = data;
    }
    public Result(StatusCode state, String msg, T data) {
        this.serial = state.getSerial();
        this.code = state.getCode();
        this.message = new StringBuilder().append(state.getDesc()).append(msg).toString();
        this.data = data;
    }

    // 成功响应
    public static <T> Result<T> success() {
        return new Result<>(StatusCode.SUCCESS, null);
    }
    public static <T> Result<T> success(T data) {
        return new Result<>(StatusCode.SUCCESS, data);
    }
    public static <T> Result<T> success(StatusCode state, T data) {
        return new Result<>(state, data);
    }
    public static <T> Result<T> success(StatusCode state, String msg, T data) {
        return new Result<>(state, msg, data);
    }

    // 错误响应
    public static <T> Result<T> error(StatusCode state) {
        return new Result<>(state.getSerial(), 1, state.getDesc(), null);
    }
    public static <T> Result<T> error(StatusCode state, String message) {
        return new Result<>(state, message,null);
    }
    public static <T> Result<T> error(StatusCode state, String msg, T data) {
        return new Result<>(state, msg, data);
    }
}
